using System;
using System.Collections.Generic;
using System.Text;
using LispInterpretor.HelperClasses;
using LispInterpretor.InBuiltProcedures;

namespace LispInterpretor.Parsers
{
    public class LambdaParser : IParser
    {
        private const string LAMBDA_IDENTIFIER = "\\";
        #region IParser Members

        public bool Is(string text, Environment environment)
        {
            try
            {
                return string.Compare(ParserHelper.ParseFuncName(text), LAMBDA_IDENTIFIER, true) == 0;
            }
            catch (InvalidProgramException)
            {
                return false;
            }
        }

        public object Evaluate(string text, Environment environment)
        {
            IList<string> parseArgument = ParserHelper.ParseArguments(text);

            Check.IsTrue(parseArgument.Count == 3, "A lambda expression must contain exactly two arguments.");

            return new UserDefinedProcedure(parseArgument[1], parseArgument[2], environment); 
        }

        #endregion
    }
}
